package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.address.TbAddress;
import com.dongdongshop.pojo.admin.TbUser;
import com.dongdongshop.service.address.AddressService;
import com.dongdongshop.util.ShiroUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {
    private Logger logger = LoggerFactory.getLogger(AddressController.class);
    @Reference
    private AddressService addressService;
    @RequestMapping("address")
    public String toAddress(){
        return "home-setting-address";
    }
    //获取地址信息
    @RequestMapping("listAddressByUserId")
    @ResponseBody
    public Result listAddressByUserId(){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            List<TbAddress> addressList = addressService.listAddressByUserId(user.getUsername());
            return Result.ok().setData(addressList);

        }catch (Exception e){
            logger.error("查询地址错误",e);
            return Result.fail();
        }
    }
    @RequestMapping("insertAddress")
    @ResponseBody
    public Result insertAddress(TbAddress tbAddress){
        try {
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            tbAddress.setUserId(user.getUsername());
            int i = addressService.insertAddress(tbAddress);
            return Result.ok();
        }catch (Exception e){
            logger.error("添加地址错误",e);
            return Result.fail();
        }
    }
}
